package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.requests.DirectEditRequest;

import edu.pku.sei.modeler.editpolicies.NestedNodeLayoutEditPolicy;
import edu.pku.sei.modeler.editpolicies.UMLDiagramGraphicalNodeEditPolicy;
import edu.pku.sei.modeler.figures.ComplexStateFigure;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.structure.StructurePackage;
import edu.pku.sei.modeler.model.uml.BehaviorStateMachines.State;
import edu.pku.sei.modeler.model.uml.BehaviorStateMachines.StateMachine;
import edu.pku.sei.modeler.uml.commands.ChangeNameCommand;
import edu.pku.sei.modeler.uml.modelHelper.ModelPackage;

public class StateMachineEditpart extends AbstractUMLNodeEditPart {
	public StateMachineEditpart(MetaShapeElement model) {
		super(model);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		return new ComplexStateFigure();
	}

	protected DirectEditPolicy createDirectEditPolicy() {
		return new DirectEditPolicy() {

			@Override
			protected Command getDirectEditCommand(DirectEditRequest arg0) {
				return new ChangeNameCommand(getStateMachine(), (String) arg0
						.getCellEditor().getValue());
			}

			@Override
			protected void showCurrentEditValue(DirectEditRequest arg0) {
				IFigure f = getDirectEditFigure();
				if (f instanceof Label) {
					((Label) f).setText(getStateMachine().getName());
				}
			}

		};
	}

	@Override
	protected IFigure getDirectEditFigure() {
		return ((ComplexStateFigure) this.getFigure()).getNameLable();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.LAYOUT_ROLE,
				new NestedNodeLayoutEditPolicy(((MetaNode)this.getModel()).getDiagram().getType())); /*
																	 * {
																	 * 
																	 * @Override
																	 * protected
																	 * Command
																	 * getCreateCommand
																	 * (
																	 * CreateRequest
																	 * request)
																	 * {
																	 * MetaNode
																	 * node =
																	 * (MetaNode
																	 * )
																	 * request.
																	 * getNewObject
																	 * ();
																	 * MetaNode
																	 * hostNode
																	 * =
																	 * (MetaNode
																	 * )
																	 * this.getHost
																	 * (
																	 * ).getModel
																	 * ();
																	 * 
																	 * return
																	 * StateMachineElementCreateCommandFactory
																	 * .
																	 * eINSTANCE
																	 * .
																	 * createCommand
																	 * (node,
																	 * hostNode,
																	 * (
																	 * Rectangle
																	 * )
																	 * getConstraintFor
																	 * (
																	 * request))
																	 * ; } });
																	 */
	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		super.handlePropertyChanged(event);
//		if (ModelPackage.NAMESPACE_OWNEDMEMBER.equals(event.getPropertyName())
//				|| StructurePackage.DIAGRAM_NODES.equals(event
//						.getPropertyName())) {
//			this.refreshChildren();
//		}
		refreshVisuals();
	}

	private StateMachine getStateMachine() {
		return (StateMachine) ((MetaNode) getModel()).getModelElement();
	}

	protected void refreshVisuals() {
		super.refreshVisuals();
		super.refreshChildren();
		StateMachine modelEle = (StateMachine) getNode().getModelElement();

		ComplexStateFigure figure = (ComplexStateFigure) getFigure();
		figure.getNameLable().setText("" + modelEle.getName());

	}

	@Override
	public IFigure getContentPane() {
		return ((ComplexStateFigure) getFigure()).getBody();
	}
}
